#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->stackedWidget->setCurrentWidget(ui->pagMain);

    QStandardItemModel* model=new QStandardItemModel(this);
    model->setColumnCount(8);
    model->setHeaderData(0,Qt::Horizontal,"编号");
    model->setHeaderData(1,Qt::Horizontal,"经度");
    model->setHeaderData(2,Qt::Horizontal,"纬度");
    model->setHeaderData(3,Qt::Horizontal,"年");
    model->setHeaderData(4,Qt::Horizontal,"月");
    model->setHeaderData(5,Qt::Horizontal,"日");
    model->setHeaderData(6,Qt::Horizontal,"时");
    model->setHeaderData(7,Qt::Horizontal,"分");
    ui->tvMain->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvMain->setModel(model);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_btnCal_clicked()
{
    QAbstractItemModel* model=ui->tvMain->model();
    for(int i=0;i<model->rowCount();++i){
        ui->textBrowser->append("\n\n编号:"+model->data(model->index(i,0)).toString());
        double longitude=model->data(model->index(i,1)).toDouble();
        double latitude=model->data(model->index(i,2)).toDouble();

        int year=model->data(model->index(i,3)).toInt();
        int month=model->data(model->index(i,4)).toInt();
        int day=model->data(model->index(i,5)).toInt();
        int start_hour=model->data(model->index(i,6)).toInt();
        int start_minute=model->data(model->index(i,7)).toInt();
        int end_hour, end_minute, interval_minute;
        char repeat_mode = 'y';

        double alpha, gamma, Hs_sunrise, Hs_sunset;
        int n;

        calculateSolarPosition(longitude, latitude, year, month, day, start_hour, start_minute, 0, alpha, gamma, Hs_sunrise, Hs_sunset, n);

        ui->textBrowser->append( "日期序数n：" +QString::number(n) );
         ui->textBrowser->append( "日出时间：" +QString::number(static_cast<int>(Hs_sunrise))+" : "+ QString::number(static_cast<int>((Hs_sunrise - static_cast<int>(Hs_sunrise)) * 60)));
        ui->textBrowser->append( "日落时间：" +QString::number(static_cast<int>(Hs_sunset))+" : "+QString::number(static_cast<int>((Hs_sunset - static_cast<int>(Hs_sunset)) * 60)) );
         ui->textBrowser->append( "太阳高度角：" +QString::number(alpha) +"°" );
         ui->textBrowser->append( "太阳方位角：" +QString::number(gamma) +"°");
    }

}


void MainWindow::on_btnAppend_clicked()
{
    QAbstractItemModel* model=ui->tvMain->model();
    model->insertRow(model->rowCount());
    if(model->rowCount()>=2){
        model->setData(model->index(model->rowCount()-1,0),model->data(model->index(model->rowCount()-2,0)).toInt()+1);
    }else{
        model->setData(model->index(model->rowCount()-1,0),1);
    }

}


void MainWindow::on_btnDelete_clicked()
{
    QAbstractItemModel* model=ui->tvMain->model();
    QModelIndex index=ui->tvMain->currentIndex();
    if(index.isValid()){
        model->removeRow(index.row());
        ui->tvMain->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnClear_clicked()
{
    QAbstractItemModel* model=ui->tvMain->model();
    model->removeRows(0,model->rowCount());
}


void MainWindow::on_pushButton_clicked()
{
    ui->textBrowser->clear();
}


void MainWindow::on_pushButton_2_clicked()
{
    adds* a=new adds();
    connect(a,SIGNAL(add(double,double,QString,int,int,int,int,int)),this,SLOT(onAdd(double,double,QString,int,int,int,int,int)));
    a->show();
}

void MainWindow::onAdd(double jd, double wd, QString ymd, int sh, int sm, int eh, int em, int jg)
{
    int startTime=sh*60+sm;
    int endTime=eh*60+em;
    QAbstractItemModel* model=ui->tvMain->model();
    QDateTime dt=QDateTime::fromString(ymd,"yyyy-MM-dd");
    for(;startTime<=endTime;startTime+=jg){
        model->insertRow(model->rowCount());
        if(model->rowCount()>=2){
            model->setData(model->index(model->rowCount()-1,0),model->data(model->index(model->rowCount()-2,0)).toInt()+1);
        }else{
            model->setData(model->index(model->rowCount()-1,0),1);
        }

        model->setData(model->index(model->rowCount()-1,1),jd);
        model->setData(model->index(model->rowCount()-1,2),wd);
        model->setData(model->index(model->rowCount()-1,3),dt.toString("yyyy"));
        model->setData(model->index(model->rowCount()-1,4),dt.toString("MM"));
        model->setData(model->index(model->rowCount()-1,5),dt.toString("dd"));
        model->setData(model->index(model->rowCount()-1,6),startTime/60);
        model->setData(model->index(model->rowCount()-1,7),startTime%60);
    }
}


void MainWindow::on_pushButton_3_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagSignal);
}


void MainWindow::on_pushButton_4_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMulti);
}


void MainWindow::on_calendarWidget_clicked(const QDate &date)
{
    ui->lineEdit->setText(date.toString("yyyy-MM-dd"));
}


void MainWindow::on_pushButton_5_clicked()
{
    double longitude=ui->doubleSpinBox->value();
    double latitude=ui->doubleSpinBox_2->value();

    QDate dt=QDate::fromString(ui->lineEdit->text(),"yyyy-MM-dd");
    int year=dt.toString("yyyy").toInt();
    int month=dt.toString("MM").toInt();
    int day=dt.toString("dd").toInt();
    int start_hour=ui->spinBox->value();
    int start_minute=ui->spinBox_2->value();
    int end_hour, end_minute, interval_minute;
    char repeat_mode = 'y';

    double alpha, gamma, Hs_sunrise, Hs_sunset;
    int n;

    calculateSolarPosition(longitude, latitude, year, month, day, start_hour, start_minute, 0, alpha, gamma, Hs_sunrise, Hs_sunset, n);

    ui->textBrowser_2->append( "日期序数n：" +QString::number(n) );
    ui->textBrowser_2->append( "日出时间：" +QString::number(static_cast<int>(Hs_sunrise))+" : "+ QString::number(static_cast<int>((Hs_sunrise - static_cast<int>(Hs_sunrise)) * 60)));
    ui->textBrowser_2->append( "日落时间：" +QString::number(static_cast<int>(Hs_sunset))+" : "+QString::number(static_cast<int>((Hs_sunset - static_cast<int>(Hs_sunset)) * 60)) );
    ui->textBrowser_2->append( "太阳高度角：" +QString::number(alpha) +"°" );
    ui->textBrowser_2->append( "太阳方位角：" +QString::number(gamma) +"°");
}


void MainWindow::on_pushButton_6_clicked()
{
    ui->textBrowser_2->clear();
}

